function [data2] = loadDataFilesCtsTime_acctCreation(dataId)

if dataId == 3
	timeAggLevel = 'day';
	geoAggLevel =  'CantonId';
	
	% Dataset for covariates
	datasetName = 'CantonId_day';
	datasetFile = sprintf('../../data/data_mat/covariates/covariates_%s.mat', datasetName);

	% Define userDataFile, providerDataFile and inputTransactionFile (output by data_cleaning)
	userDataFile         = sprintf('../../data/data_csv/platformData/userData.csv');
	providerDataFile     = sprintf('../../data/data_csv/platformData/providerData.csv');
	inputTransactionFile = sprintf('../../data/data_csv/platformData/evaluationsData.csv');
	geoDefFile           = sprintf('../../data/data_csv/covariates/Scope_CantonId.csv');
end

if strcmp(timeAggLevel, 'week'); tt_subset_idxes = 1:68;  end;
if strcmp(timeAggLevel, 'day');  tt_subset_idxes = 1:476; end;

locationAggregationsFolder = sprintf('../../data/data_mat/timeLocationAggregations/location/%s', geoAggLevel);
timeAggregationsFolder     = sprintf('../../data/data_mat/timeLocationAggregations/time/%s',     timeAggLevel);


%%%%%%%%%% Make table of timestamps for first-time activity

%%%%% Read input files
% Read geo-def file and get geo codes that will allow me to convert from geoCode to integer
opts_geo = detectImportOptions(geoDefFile);
opts_geo = setvartype(opts_geo, 'CantonId', 'char');
Mgeo = readtable(geoDefFile, opts_geo);
geoLabels = unique(table2cell(Mgeo(:,1)));
clear Mgeo opts_geo;
K = length(geoLabels);

%%% Read user data + convert location to integer kk + get unique userIds
opts = detectImportOptions(userDataFile); opts = setvartype(opts, geoAggLevel, 'char');
userData = readtable(userDataFile, opts);
userData = userData(:,{'UserId', 'AccountCreationDate_julian', geoAggLevel});
clear opts;
userIds = table2array(userData(:,'UserId'));
NumUsers = length(userIds);


[flag, user2geoIdx] = ismember(table2cell(userData(:,geoAggLevel)), geoLabels);
assert(all(flag));
M_user_kk = table(user2geoIdx); M_user_kk.Properties.VariableNames = {'kk'};
userData = [userData M_user_kk];
userData = userData(:,[1 2 4]);
clear flag user2geoIdx M_user_kk;

%%% Read providerData
opts = detectImportOptions(providerDataFile); opts = setvartype(opts, geoAggLevel, 'char');
providerData = readtable(providerDataFile, opts);
providerData = providerData(:,{'UserId', 'providerCreationDate_julian', 'providerDeletionDate_julian'});
clear opts myUserIds flag myUserIdxes myTable;

%%% Read transactionData + determine date of first transaction for each user who played the role of consumer at least once
% Read transactionsData file
transactionsData = readtable(inputTransactionFile);

consumerData = transactionsData(:,{'eventDate_julian', 'ConsumerId', 'ProviderId'});
a = table2array(consumerData(:,'eventDate_julian')); assert(all(a(2:end) >= a(1:end-1))); clear a; % Check that transactions are ordered by incr. date.
% Retain first ransaction for each consumer
myUserIds = table2array(consumerData(:,{'ConsumerId'}));
myUnqUserIds = unique(myUserIds);
[flag,myUserIdxes] = ismember(myUserIds, myUnqUserIds);
assert(all(flag));
a = accumarray(myUserIdxes, [1:length(myUserIdxes)]', [length(myUnqUserIds) 1], @min);
consumerData = consumerData(a,[2 1 3]);
consumerData.Properties.VariableNames = {'UserId', 'Consumer1stTransacDate_julian', 'Consumer1stTransac_idOfProvider'};
% Get location of the provider met by the user for his first transaction as consumer
tmpProviderIds = table2array(consumerData(:,'Consumer1stTransac_idOfProvider'));
[flag2,tmpProviderIds_userIdxes] = ismember(tmpProviderIds, userIds);
assert(all(flag2)); clear flag2;
Consumer1stTransac_carLocation_k2 = table2array(userData(tmpProviderIds_userIdxes,'kk'));
consumerData = [consumerData table(Consumer1stTransac_carLocation_k2)];
consumerData = consumerData(:,{'UserId', 'Consumer1stTransacDate_julian', 'Consumer1stTransac_carLocation_k2'});
% Clean up
clear myUserIds myUnqUserIds flag myUserIdxes a tmpProviderIds tmpProviderIds_userIdxes Consumer1stTransac_carLocation_k2;

% Retain first transaction for each provider
providerTransacData = transactionsData(:,{'eventDate_julian', 'ProviderId'});
a = table2array(providerTransacData(:,'eventDate_julian')); assert(all(a(2:end) >= a(1:end-1))); clear a; % Check that transactions are ordered by incr. date.
% Retain first transaction for each provider
myUserIds = table2array(providerTransacData(:,{'ProviderId'}));
myUnqUserIds = unique(myUserIds);
[flag,myUserIdxes] = ismember(myUserIds, myUnqUserIds);
assert(all(flag));
a = accumarray(myUserIdxes, [1:length(myUserIdxes)]', [length(myUnqUserIds) 1], @min);
providerTransacData = providerTransacData(a,[2 1]);
providerTransacData.Properties.VariableNames = {'UserId', 'Provider1stTransacDate_julian'};
clear myUserIds myUnqUserIds flag myUserIdxes a ;

clear transactionsData;


%%% Merge userData, providerData, consumerData
M = outerjoin(userData, consumerData);
M.Properties.VariableNames{1} = 'UserId';
M = outerjoin(M, providerTransacData);
M.Properties.VariableNames{1} = 'UserId';
M = outerjoin(M, providerData);
M.Properties.VariableNames{1} = 'UserId';
M = M(:, {'UserId', 'AccountCreationDate_julian', 'kk', 'Consumer1stTransacDate_julian', 'Provider1stTransacDate_julian', 'providerCreationDate_julian', 'providerDeletionDate_julian'});

% Categorize users based on first behavior (consumer transaction or "providerCreationDate_julian")
userType = zeros(NumUsers, 1);
consumer1stDate = table2array(M(:,'Consumer1stTransacDate_julian'));
provider1stDate  = table2array(M(:,'providerCreationDate_julian'));
flag1 = isnan(consumer1stDate) & isnan(provider1stDate);						 % user with no recorded activity
flag2 = ~flag1 & (isnan(provider1stDate) | consumer1stDate < provider1stDate);	 % consumer
flag3 = ~flag1 & (isnan(consumer1stDate) | provider1stDate <= consumer1stDate); % provider
assert(all(flag1+flag2+flag3 == 1)); % Check that all users are set to one (and exactly one) user type
userType(flag1) = 1;
userType(flag2) = 2;
userType(flag3) = 3;
M = [M table(userType)];


%%%%%%%%%% Make groups of users based on activity
%% Define groups of users based on any activity recorded within the first 60 days (beyond that, it's not clear why account was created)
delayMax = 60;
acctCreationDate       = table2array(M(:,'AccountCreationDate_julian'));
consumer1stTransacDate = table2array(M(:,'Consumer1stTransacDate_julian'));
providerCreationDate   = table2array(M(:,'providerCreationDate_julian'));
userType             = table2array(M(:,'userType'));
flag2 = (userType == 2) & consumer1stTransacDate - acctCreationDate < delayMax & consumer1stTransacDate > acctCreationDate; % definition of consumer
flag3 = (userType == 3) & providerCreationDate   - acctCreationDate < delayMax & providerCreationDate   > acctCreationDate; % definition of provider
flag_bad = consumer1stTransacDate < acctCreationDate | providerCreationDate < acctCreationDate;
flag1 = ~flag_bad & ~flag2 & ~flag3; % definition of account created with no immediate activity


%%% Record events in a table (account creations by both types + provider exit) with a corresponding code indicating type of event and kk
% Record account creations by people showing no activity
myEventsTable = M(flag1,{'AccountCreationDate_julian', 'kk'});
eventType = 1*ones(size(myEventsTable,1),1);
myEventsTable = [myEventsTable table(eventType)];
clear eventType;
allEvents = myEventsTable;

% Record account creations by consumers
myEventsTable = M(flag2,{'AccountCreationDate_julian', 'kk'});
eventType = 2*ones(size(myEventsTable,1),1);
myEventsTable = [myEventsTable table(eventType)];
clear eventType;
allEvents = [allEvents; myEventsTable];

% Record account creations by providers
myEventsTable = M(flag3,{'AccountCreationDate_julian', 'kk'});
eventType = 3*ones(size(myEventsTable,1),1);
myEventsTable = [myEventsTable table(eventType)];
clear eventType;
allEvents = [allEvents; myEventsTable];

% Record provider exits
flag4 = flag3 & ~isnan(table2array(M(:,'providerDeletionDate_julian')));
myEventsTable = M(flag4,{'providerDeletionDate_julian', 'kk'});
eventType = 4*ones(size(myEventsTable,1),1);
myEventsTable = [myEventsTable table(eventType)];
clear eventType;
allEvents.Properties.VariableNames = {'Timestamp', 'kk', 'eventType'};
myEventsTable.Properties.VariableNames = {'Timestamp', 'kk', 'eventType'};
allEvents = [allEvents; myEventsTable];

assert(all(~isnan(table2array(allEvents(:,1)))));

%%%% By now: allEvents is a table: Timestamp | kk | eventType
% where eventType = 1 for account creation of user who showed no activity by end of period
% where eventType = 2 for account creation of consumer based on 1st recorded activity
% where eventType = 3 for account creation of provider based on 1st recorded activity
% where eventType = 4 for provider deletion (only for those users tagged as providers based on 1st recorded activity)

% Make an array out of allEvents
allEventsData = table2array(allEvents);
clear allEvents;

%%% Remove events that happen after window of interest
after_deadline = allEventsData(:,1) > max(tt_subset_idxes);
allEventsData = allEventsData(~after_deadline,:);

% Separate events that happened before vs during the time window of interest
before_start = allEventsData(:,1) < min(tt_subset_idxes)-1;
priorEvents = allEventsData(before_start, :);   % before window
allEventsData = allEventsData(~before_start,:); % during window

% New events: add events of type=5 for day changes
periodChanges = tt_subset_idxes';
NumPeriodChanges   = length(tt_subset_idxes);
periodChangeData   = [periodChanges -1*ones(NumPeriodChanges,1) 5*ones(NumPeriodChanges,1)];    % NumPeriods x 1
allEventsData = [allEventsData; periodChangeData]; % NumEvents x 3
clear periodChangeData NumPeriodChanges;

%%% Get initial installed bases
% initLaggedInactiveUsers
priorInactiveUserCreations = priorEvents(priorEvents(:,3) == 1,:);
priorInactiveUserCreations_kk = priorInactiveUserCreations(:,2); clear priorInactiveUserCreations;
initLaggedInactiveUsers = accumarray(priorInactiveUserCreations_kk, priorInactiveUserCreations_kk, [K 1], @length); % K x 1

% initLaggedConsumers
priorConsumerCreations = priorEvents(priorEvents(:,3) == 2,:);
priorConsumerCreations_kk = priorConsumerCreations(:,2); clear priorConsumerCreations;
initLaggedConsumers = accumarray(priorConsumerCreations_kk, priorConsumerCreations_kk, [K 1], @length); % K x 1

% initLaggedProviders
priorProviderCreations = priorEvents(priorEvents(:,3) == 3,:);
priorProviderDeletions = priorEvents(priorEvents(:,3) == 4,:);
priorProviderCreations_kk = priorProviderCreations(:,2); clear priorProviderCreations;
priorProviderDeletions_kk = priorProviderDeletions(:,2); clear priorProviderDeletions;
initLaggedProviders_created = accumarray(priorProviderCreations_kk, priorProviderCreations_kk, [K 1], @length);
initLaggedProviders_deleted = accumarray(priorProviderDeletions_kk, priorProviderDeletions_kk, [K 1], @length);
initLaggedProviders = initLaggedProviders_created - initLaggedProviders_deleted;  % K x 1


%%% CONSTRUCT SPELLS FROM ORDERED EVENTS %%%%%%%%%%
% Sort allEventsData by increasing timestamp
[~,sortidxes] = sort(allEventsData(:,1));
allEventsData = allEventsData(sortidxes,:);
clear sortidxes;

%%% Compute Y
NumSpells = size(allEventsData,1);
Y.inactiveUserCreations = sparse(NumSpells, K); % NumSpells x K
Y.consumerCreations     = sparse(NumSpells, K); % NumSpells x K
Y.providerCreations      = sparse(NumSpells, K); % NumSpells x K
Y.providerDeletions      = sparse(NumSpells, K); % NumSpells x K

for tt = 1:NumSpells
	if allEventsData(tt,3) == 1
		kk = allEventsData(tt,2);
		Y.inactiveUserCreations(tt, kk) = 1;
	end
	if allEventsData(tt,3) == 2 % consumer creation
		kk = allEventsData(tt,2);
		Y.consumerCreations(tt,kk) = 1;
	end
	if allEventsData(tt,3) == 3 % provider creation
		kk = allEventsData(tt,2);
		Y.providerCreations(tt,kk) = 1;
	end
	if allEventsData(tt,3) == 4 % provider deletion
		kk = allEventsData(tt,2);
		Y.providerDeletions(tt,kk) = 1;
	end
end
clear kk;

%%% Compute lagged installed bases
laggedInactiveUsers = initLaggedInactiveUsers' + cumsum(Y.inactiveUserCreations, 1) - Y.inactiveUserCreations; % NumSpells x K
laggedConsumers     = initLaggedConsumers' + cumsum(Y.consumerCreations, 1) - Y.consumerCreations; % NumSpells x K
providerDeltas  = Y.providerCreations - Y.providerDeletions;
laggedProviders = initLaggedProviders' + cumsum(providerDeltas, 1) - providerDeltas; % NumSpells x K
clear providerDeltas;

% Compute spell durations
event_dates = allEventsData(:,1);         % NumSpells x 1
spellStarts = [0; event_dates(1:end-1)];  % NumSpells x 1
spellEnds   = event_dates;                % NumSpells x 1
spellDurations = spellEnds - spellStarts; % NumSpells x 1
clear event_dates;

% Make mapping from spell to day (at start of spell) --> I am assuming periodicity at day level here!
spell2period = make_integers(floor(spellStarts) +  1); % NumSpells x 1 (gives values between 1 and NumPeriods)

%%%% Store everything
ctsTimeData.laggedInactiveUsers = laggedInactiveUsers;     % NumSpells x K
ctsTimeData.laggedConsumers     = laggedConsumers;         % NumSpells x K
ctsTimeData.laggedProviders     = laggedProviders;         % NumSpells x K
ctsTimeData.NewInactiveUsers    = Y.inactiveUserCreations; % NumSpells x K
ctsTimeData.NewConsumers        = Y.consumerCreations;     % NumSpells x K
ctsTimeData.NewProviders        = Y.providerCreations;     % NumSpells x K
ctsTimeData.DeletedProviders    = Y.providerDeletions;     % NumSpells x K
ctsTimeData.spellDurations      = spellDurations;          % NumSpells x K
ctsTimeData.spell2period        = spell2period;            % NumSpells x K


%%%%%%%%%% LOAD PERIODIC DATA of covariates %%%%%%%%%
configPeriodicData.datasetFile                = datasetFile;
configPeriodicData.timeAggLevel               = timeAggLevel;
configPeriodicData.geoAggLevel                = geoAggLevel;
configPeriodicData.surfaceVar_Zname           = 'Area';
configPeriodicData.laggedAdoptionEffects      = {{}}; % (ignored any way...)
configPeriodicData.locationAggregationsFolder = locationAggregationsFolder;
configPeriodicData.timeAggregationsFolder     = timeAggregationsFolder;


data_tmp = loadPeriodicData(configPeriodicData, tt_subset_idxes);
periodicData      = data_tmp.periodicData;
periodLabels      = data_tmp.periodLabels;
population        = data_tmp.population;
surfaces          = data_tmp.surfaces;
incomingCommuters = data_tmp.incomingCommuters;
touristBeds       = data_tmp.touristBeds;
surfaceName       = data_tmp.surfaceName;

assert(isequal(data_tmp.geoLabels, geoLabels));

clear data_tmp;


%%%%%% Store everything into data2
data2.ctsTimeData  = ctsTimeData;
data2.periodicData = periodicData;

data2.geoLabels    = geoLabels;
data2.periodLabels  = periodLabels;

data2.population   = population;
data2.surfaces     = surfaces;
data2.incomingCommuters = incomingCommuters;
data2.touristBeds       = touristBeds;
data2.surfaceName       = surfaceName;

end
